/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ServiciosGUI.java
 *
 * Created on 27/06/2010, 08:53:30 AM
 */

package frontera;

import entidad.Servicios;
import control.ServiciosCTR;
import entidad.TiposServicios;
import java.util.Iterator;
import javax.swing.table.DefaultTableModel;
//import javax.swing.JOptionPane;

/**
 *
 * @author Administrador
 */
public class ServiciosGUI extends javax.swing.JPanel {
    public ServiciosCTR serviciosCTR;
    public String accion;
    public int indice;

    /** Creates new form ServiciosGUI */
    public ServiciosGUI() {
        serviciosCTR = new ServiciosCTR();
        initComponents();
        cargarTiposServicios();
        iniciar();
   }

    public void iniciar(){
        accion = "insertar";
        llenarTabla();
    }

    public void llenarTabla() {
        Iterator iteratorServicios = serviciosCTR.getServicios().iterator();

        Servicios auxServicios;
        DefaultTableModel _data = new DefaultTableModel(new Object []{"Codigo","Nombre","Valor","%Iva","Tipo"},0);
        while(iteratorServicios.hasNext()){
            auxServicios =  (Servicios)(iteratorServicios.next());
            _data.addRow(new Object []{auxServicios.getId(),auxServicios.getNombre(),auxServicios.getValorUnitario(),auxServicios.getIva(),auxServicios.getTipoServicio().getNombre()}) ;
            //_data.setValueAt(_aux_cliente.getNombre() , _data.getRowCount()-1 ,1) ;
        }
        serviciosSP.setModel(_data);
    }

    public void cargarTiposServicios(){
        ListaTiposServicios.removeAllItems();
        for(TiposServicios u : FramePrincipal1.sistema.getTiposServicios()){
            ListaTiposServicios.addItem(u.getNombre());
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        grid_tb = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        nombreJT = new javax.swing.JTextField();
        valorJT = new javax.swing.JTextField();
        ivaJT = new javax.swing.JTextField();
        guardarB = new javax.swing.JButton();
        LimpiarB = new javax.swing.JButton();
        EliminarB = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        serviciosSP = new javax.swing.JTable();
        jLabel5 = new javax.swing.JLabel();
        ListaTiposServicios = new javax.swing.JComboBox();

        grid_tb.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                grid_tbeditar(evt);
            }
        });
        jScrollPane1.setViewportView(grid_tb);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel1.setText("Gestion de Servicios");

        jLabel2.setText("Nombre");

        jLabel3.setText("Valor");

        jLabel4.setText("%Iva");

        nombreJT.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nombreJTActionPerformed(evt);
            }
        });

        valorJT.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                valorJTActionPerformed(evt);
            }
        });

        guardarB.setText("Guardar");
        guardarB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                guardarBActionPerformed(evt);
            }
        });

        LimpiarB.setText("Limpiar");
        LimpiarB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LimpiarBActionPerformed(evt);
            }
        });

        EliminarB.setText("Eliminar");
        EliminarB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                EliminarBActionPerformed(evt);
            }
        });

        serviciosSP.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                serviciosSPeditar(evt);
            }
        });
        jScrollPane2.setViewportView(serviciosSP);

        jLabel5.setText("Concepto");

        ListaTiposServicios.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ListaTiposServiciosActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 558, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(158, 158, 158)
                        .addComponent(guardarB)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(LimpiarB)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(EliminarB))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(21, 21, 21)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE)
                                    .addComponent(jLabel2)))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(19, 19, 19)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel4))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(84, 84, 84)
                                        .addComponent(jLabel1))
                                    .addComponent(valorJT, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(ivaJT, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(nombreJT, javax.swing.GroupLayout.PREFERRED_SIZE, 307, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 85, Short.MAX_VALUE))
                            .addComponent(ListaTiposServicios, 0, 392, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nombreJT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(valorJT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ivaJT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ListaTiposServicios, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(guardarB)
                    .addComponent(LimpiarB)
                    .addComponent(EliminarB))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(12, 12, 12))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void nombreJTActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nombreJTActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_nombreJTActionPerformed

    private void valorJTActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_valorJTActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_valorJTActionPerformed

    private void EliminarBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_EliminarBActionPerformed
        try{
            if ( nombreJT.getText() != "" ){
                serviciosCTR.borrar(nombreJT.getText());
                llenarTabla();
                limpiar();
            }
        }catch(Exception e){

        }
}//GEN-LAST:event_EliminarBActionPerformed

    private void guardarBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guardarBActionPerformed
        // TODO add your handling code here:
        // TODO add your handling code here:
        Servicios servicios = new Servicios();
            TiposServicios t1 = new TiposServicios();
            t1.setNombre(ListaTiposServicios.getSelectedItem().toString());
            
            int id = 0;
            for(TiposServicios t : FramePrincipal1.sistema.getTiposServicios()){
            if(t.getNombre() == ListaTiposServicios.getSelectedItem()) id = t.getId();
            }
            t1.setId(id);
            servicios.setTipoServicio(t1);
            servicios.setNombre(nombreJT.getText());
            servicios.setValorUnitario(Integer.parseInt(valorJT.getText()));
            servicios.setIva(Integer.parseInt(ivaJT.getText()));
            
        if (accion == "insertar"){
            int j = serviciosSP.getRowCount() + 1;
            System.out.println(j);
            servicios.setId(j);
            servicios.setCantidad(1);
            servicios.setEstado(true);
            serviciosCTR.adicionar(servicios);
        }

        if (accion == "editar"){
            serviciosCTR.getServicios().get(indice).setNombre(servicios.getNombre());
            serviciosCTR.getServicios().get(indice).setValorUnitario(servicios.getValorUnitario());
            serviciosCTR.getServicios().get(indice).setIva(servicios.getIva());
            serviciosCTR.getServicios().get(indice).setCantidad(servicios.getCantidad());
            serviciosCTR.getServicios().get(indice).setEstado(servicios.isEstado());
            serviciosCTR.getServicios().get(indice).setTipoServicio(t1);
        }
        llenarTabla();
        limpiar();
        //show_msn(servicios.insert_ok );
}//GEN-LAST:event_guardarBActionPerformed

    private void limpiar() {
        nombreJT.setText("");
        valorJT.setText("");
        ivaJT.setText("");
        accion = "insertar";
    }

    private void LimpiarBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_LimpiarBActionPerformed
        limpiar();
        accion = "insertar";
}//GEN-LAST:event_LimpiarBActionPerformed

    private void grid_tbeditar(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_grid_tbeditar
        // TODO add your handling code here:
    }//GEN-LAST:event_grid_tbeditar

    private void serviciosSPeditar(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_serviciosSPeditar
        // TODO add your handling code here:
        accion = "editar";
        int fila = serviciosSP.rowAtPoint(evt.getPoint());
        int columna = serviciosSP.columnAtPoint(evt.getPoint());
        if (fila > -1) {
            nombreJT.setText(serviciosSP.getModel().getValueAt(fila, 1).toString());
            valorJT.setText(serviciosSP.getModel().getValueAt(fila, 2).toString());
            ivaJT.setText(serviciosSP.getModel().getValueAt(fila, 3).toString());
            //nombreJT.getText();
            System.out.println(nombreJT.getText());

            Servicios s1 = new Servicios();

            s1 = serviciosCTR.buscarServiciosByNombre(nombreJT.getText());

            TiposServicios t1 = new TiposServicios();
            
            t1.setId(s1.getTipoServicio().getId());
            t1.setNombre(s1.getTipoServicio().getNombre());
                      
            System.out.println(t1.getId());
            System.out.println(t1.getNombre());

            //ListaTiposServicios.setSelectedItem(t1);
            ListaTiposServicios.setSelectedIndex(t1.getId()-1);

            indice = serviciosCTR.buscarPorIndice(nombreJT.getText());
        }
}//GEN-LAST:event_serviciosSPeditar

    private void ListaTiposServiciosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ListaTiposServiciosActionPerformed
        int id = 0;
        for(TiposServicios t : FramePrincipal1.sistema.getTiposServicios()){
            if(t.getNombre() == ListaTiposServicios.getSelectedItem()) id = t.getId();
        }
}//GEN-LAST:event_ListaTiposServiciosActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton EliminarB;
    private javax.swing.JButton LimpiarB;
    private javax.swing.JComboBox ListaTiposServicios;
    private javax.swing.JTable grid_tb;
    private javax.swing.JButton guardarB;
    private javax.swing.JTextField ivaJT;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField nombreJT;
    private javax.swing.JTable serviciosSP;
    private javax.swing.JTextField valorJT;
    // End of variables declaration//GEN-END:variables

}
